package cn.micai.core.controller.admin;

import cn.micai.common.controller.BaseController;
import cn.micai.common.page.Pagination;
import cn.micai.core.bean.order.Detail;
import cn.micai.core.bean.order.Order;
import cn.micai.core.bean.product.Product;
import cn.micai.core.bean.user.Addr;
import cn.micai.core.query.order.DetailQuery;
import cn.micai.core.query.order.OrderQuery;
import cn.micai.core.query.user.AddrQuery;
import cn.micai.core.service.order.DetailService;
import cn.micai.core.service.order.OrderService;
import cn.micai.core.service.user.AddrService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 订单列表
 * 订单查看
 * @author zhaoxinguo on 2017/8/7.
 */
@Controller
@RequestMapping(value = "/control")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired
    private DetailService detailService;

    @Autowired
    private AddrService addrService;

    //订单列表
    //支付状态
    //订单状态
    @RequestMapping(value = "/order/list_0.do")
    public String list(Integer pageNo,Integer state,ModelMap model){

        //分页的参数
        StringBuilder params = new StringBuilder();

        //订单条件对象
        OrderQuery orderQuery = new OrderQuery();

        //1.判断支付状态是否为Null
        /*if(isPaiy != null && !isPaiy.equals("")){
            orderQuery.setIsPaiy(isPaiy);
//            // 要求模糊查询
//            params.append("is_paiy=").append(isPaiy);
//            //回显查询条件
            model.addAttribute("isPaiy", isPaiy);
        }*/
        orderQuery.setIsPaiy(0);//货到付款

        //2.判断订单状态是否为Null
        if(state != null && !state.equals("")){
            orderQuery.setState(state);
//            // 要求模糊查询
//            params.append("state=").append(state);
//            //回显查询条件
//            model.addAttribute("state", state);
        }

        logger.info("pageNo: " + pageNo);
        //设置分页
        orderQuery.setPageNo(Pagination.cpn(pageNo));
        //设置每页数
        orderQuery.setPageSize(5);

        Pagination pagination = orderService.getOrderListWithPage(orderQuery);
        //分页页面展示
        pagination.pageView("/control/order/list_0.do", params.toString());
        model.addAttribute("pagination", pagination);

        logger.info("订单列表：" + pagination.getList().toString());

        return "order/list_0";
    }

    //订单列表
    //支付状态
    //订单状态
    @RequestMapping(value = "/order/list_1.do")
    public String list_1(Integer pageNo,Integer state,ModelMap model){

        //分页的参数
        StringBuilder params = new StringBuilder();

        //订单条件对象
        OrderQuery orderQuery = new OrderQuery();

        //1.判断支付状态是否为Null
        /*if(isPaiy != null && !isPaiy.equals("")){
            orderQuery.setIsPaiy(isPaiy);
//            // 要求模糊查询
//            params.append("is_paiy=").append(isPaiy);
//            //回显查询条件
            model.addAttribute("isPaiy", isPaiy);
        }*/
        orderQuery.setIsPaiy(1);//待付款

        //2.判断订单状态是否为Null
        if(state != null && !state.equals("")){
            orderQuery.setState(state);
//            // 要求模糊查询
//            params.append("state=").append(state);
//            //回显查询条件
//            model.addAttribute("state", state);
        }

        logger.info("pageNo: " + pageNo);
        //设置分页
        orderQuery.setPageNo(Pagination.cpn(pageNo));
        //设置每页数
        orderQuery.setPageSize(5);

        Pagination pagination = orderService.getOrderListWithPage(orderQuery);
        //分页页面展示
        pagination.pageView("/control/order/list_1.do", params.toString());
        model.addAttribute("pagination", pagination);

        logger.info("订单列表：" + pagination.getList().toString());

        return "order/list_1";
    }

    //订单列表
    //支付状态
    //订单状态
    @RequestMapping(value = "/order/list_2.do")
    public String list_2(Integer pageNo,Integer state,ModelMap model){

        //分页的参数
        StringBuilder params = new StringBuilder();

        //订单条件对象
        OrderQuery orderQuery = new OrderQuery();

        //1.判断支付状态是否为Null
        /*if(isPaiy != null && !isPaiy.equals("")){
            orderQuery.setIsPaiy(isPaiy);
//            // 要求模糊查询
//            params.append("is_paiy=").append(isPaiy);
//            //回显查询条件
            model.addAttribute("isPaiy", isPaiy);
        }*/
        orderQuery.setIsPaiy(2);//已付款

        //2.判断订单状态是否为Null
        if(state != null && !state.equals("")){
            orderQuery.setState(state);
//            // 要求模糊查询
//            params.append("state=").append(state);
//            //回显查询条件
//            model.addAttribute("state", state);
        }

        logger.info("pageNo: " + pageNo);
        //设置分页
        orderQuery.setPageNo(Pagination.cpn(pageNo));
        //设置每页数
        orderQuery.setPageSize(5);

        Pagination pagination = orderService.getOrderListWithPage(orderQuery);
        //分页页面展示
        pagination.pageView("/control/order/list_2.do", params.toString());
        model.addAttribute("pagination", pagination);

        logger.info("订单列表：" + pagination.getList().toString());

        return "order/list_2";
    }

    //订单查看
    @RequestMapping(value = "/order/view.do")
    public String view(Integer id,ModelMap model){
        //查询订单主表
        Order order = orderService.getOrderByKey(id);

        //查询订单详情表
        DetailQuery detailQuery = new DetailQuery();
        detailQuery.setOrderId(id);
        List<Detail> details = detailService.getDetailList(detailQuery);

        //收货地址
        AddrQuery addrQuery = new AddrQuery();
        addrQuery.setBuyerId(order.getBuyerId());
        addrQuery.setIsDef(1);
        List<Addr> addrs = addrService.getAddrList(addrQuery);

        //返回订单详情
        if(order != null){
            model.addAttribute("order", order);
        }
        if(addrs != null && addrs.size() > 0){
            model.addAttribute("addr", addrs.get(0));
        }
        if(details != null && details.size() > 0){
            model.addAttribute("details", details);
        }

        return "order/view";
    }

}
